package models

import (
	"gitchina/kly_service/data"
	"gitchina/kly_service/errors"
	"gitchina/kly_service/util"
)

type UpdateCarrierOwnerRQ struct {
	VehicleNo     string `json:"vehicle_no"`
	VehicleType   int    `json:"vehicle_type"`
	VehicleLength int    `json:"vehicle_len"`
	VehicleLoad   int    `json:"vehicle_load"`
}
type CarrierOwnerRS struct {
	Name               string `json:"name"`
	Stars              int    `json:"stars"`
	Phone              string `json:"-"`
	VehicleType        int    `json:"vehicle_type"`
	VehcileLen         int    `json:"vehicle_len"`
	VehicleLoad        int    `json:"vehicle_load"`
	VehicleNo          string `json:"vehicle_no"`
	VehcilePhotoUrl    string `json:"vehicle_photo_url"`
	DrivingLicenseUrl  string `json:"driving_license_url"`
	VehicleLicenseUrl1 string `json:"vehicle_license_1_url"`
	VehicleLicenseUrl2 string `json:"vehicle_license_2_url"`
}

func (m Member) UpdateCarrierOwner(mbrId ID, rq UpdateCarrierOwnerRQ) (err error) {
	var id int64
	var repo data.MemberRepo
	_, _, _, id, err = mbrId.Decode()
	if err != nil {
		util.GetLogger().Error("[model - UpdateCarrierOwner] - error. unexpected mbrId:%s", string(mbrId))
		err = errors.New(errors.MSG_Mbr_Invalid)
	} else {
		err = repo.UpdateCarrierOwner(&data.CarrierOwnerDto{
			Id:            int(id),
			VehicleType:   rq.VehicleType,
			VehicleLength: rq.VehicleLength,
			VehicleLoad:   rq.VehicleLoad,
			VehicleNo:     rq.VehicleNo,
		})
		if err != nil {
			util.GetLogger().Error("[model - UpdateCarrierOwner] - error: %s", err.Error())
			err = errors.New(errors.MSG_Mbr_Upd_Carrier_Owner_Error)
		}
	}
	return err
}

func (m Member) GetCarrierOwner(mbrId ID) (rs CarrierOwnerRS, err error) {
	var id int64
	_, _, _, id, err = mbrId.Decode()
	if err != nil {
		util.GetLogger().Error("[model-GetCarrierOwner] - unexpected mbrId:%s", string(mbrId))
		err = errors.New(errors.MSG_Mbr_Invalid)
	} else {
		var repo data.MemberRepo
		if dto, err := repo.GetCarrierOwner(int(id)); err == nil {
			rs = CarrierOwnerRS{
				Name:               dto.Name,
				Stars:              dto.Stars,
				Phone:              dto.Phone,
				VehicleType:        dto.VehicleType,
				VehcileLen:         dto.VehcileLen,
				VehicleLoad:        dto.VehicleLoad,
				VehicleNo:          dto.VehicleNo,
				VehcilePhotoUrl:    dto.VehcilePhotoUrl,
				DrivingLicenseUrl:  dto.DrivingLicenseUrl,
				VehicleLicenseUrl1: dto.VehicleLicenseUrl1,
				VehicleLicenseUrl2: dto.VehicleLicenseUrl2,
			}
		} else {
			util.GetLogger().Error("[model-GetCarrierOwner] - error: %s", err.Error())
			err = nil
		}
	}
	return
}
